﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace SpiritAlight.Animation
{
    enum AnimationMode
    {
        Once,
        Repeat,
        OnceReturn,
    }


    abstract class IAnimationSprite
    {
        virtual public AnimationMode Mode
        {
            get;
            set;
        }

        virtual public int FrameRate
        {
            get;
            set;
        }

        virtual public bool Playing
        {
            get;
            set;
        }

        virtual public int Count
        {
            get;
            set;
        }

        virtual public int Frame
        {
            get;
            set;
        }
        virtual public bool ISEnd
        {
            get;
            set;
        }
        virtual public void Play() { Playing = true; Count = Frame = 0; }
        virtual public void Start() { Playing = true; }
        virtual public void Stop() { Playing = false; }
        virtual public void End() { Playing = false; Count = Frame = 0; }

    }

    class AnimationSprite : IAnimationSprite
    {
        public AnimationSprite()
        {
            Mode = AnimationMode.Repeat;
            frameRate = 15;
            playingID = 0;
        }
        public override AnimationMode Mode
        {
            get { return mode; }
            set { mode = value; }
        }
        AnimationMode mode;

        public override int FrameRate
        {
            get { return frameRate; }
            set { frameRate = value; }
        }
        int frameRate;

        public int PlayingID
        {
            get { return playingID; }
            set 
            {
                if (value > patternList.Count - 1 || value < 0)
                    return;
                playingID = value;
            }
        }
        int playingID;

        public override bool ISEnd
        {
            get { return iSEnd; }
            set { iSEnd = value; }
        }
        bool iSEnd;
        
        public override bool Playing
        {
            get { return playing; }
            set { playing = value; }
        }
        bool playing;

        public override int Count
        {
            get { return count; }
            set { count = value; }
        }
        int count;

        public override int Frame
        {
            get { return frame; }
            set { frame = value; }
        }
        int frame;
        List<AnimationPart> patternList = new List<AnimationPart>();

        public Texture2D Animation
        {
            get
            {
                return patternList[playingID].Animation;
            }
        }

        public Texture2D this[int index]
        {
            get { return patternList[index].Animation; }
        }

        /// <summary>
        /// 新しいアニメーションパターンを追加する。
        /// </summary>
        /// <param name="fileName">読み込むアニメーションファイル</param>
        /// <returns>読み込んだパターンのインデックス</returns>
        public int LoadNewPattern(params string[] fileName)
        {
            int buf = patternList.Count;

            AnimationPart newPattern = new AnimationPart(this);
            newPattern.LoadAnimation(fileName);
            patternList.Add(newPattern);

            return buf;
        }
    }

    class NullAnimationSprite : IAnimationSprite { }
}
